Implement ostree::checksum_file_at
authorFelix Krull <f_krull@gmx.de>
Mon, 2 Sep 2019 12:39:56 +0000 (14:39 +0200)
committerColin Walters <walters@verbum.org>
Fri, 6 May 2022 16:53:54 +0000 (12:53 -0400)
rust-bindings/rust/src/functions.rs
rust-bindings/rust/tests/functions/checksum_file_at.rs [new file with mode: 0644]
rust-bindings/rust/tests/functions/mod.rs

index a017e049779ea2c55e4d6301fed11be7bbdbcbb1..5a640b2b6fe521c1a94d289b551fe7af3a82508b 100644 (file)
@@ -1,3 +1,5 @@
+#[cfg(any(feature = "v2017_13", feature = "dox"))]
+use crate::ChecksumFlags;
 use crate::{Checksum, ObjectType};
 #[cfg(feature = "futures")]
 use futures::future;
@@ -6,7 +8,7 @@ use glib::translate::*;
 use glib_sys::GFALSE;
 #[cfg(feature = "futures")]
 use std::boxed::Box as Box_;
-use std::error::Error;
+use std::error;
 use std::mem::MaybeUninit;
 use std::ptr;
 
@@ -14,7 +16,7 @@ pub fn checksum_file<P: IsA<gio::File>, Q: IsA<gio::Cancellable>>(
     f: &P,
     objtype: ObjectType,
     cancellable: Option<&Q>,
-) -> Result<Checksum, Box<dyn Error>> {
+) -> Result<Checksum, Box<dyn error::Error>> {
     unsafe {
         let mut out_csum = ptr::null_mut();
         let mut error = ptr::null_mut();
@@ -32,7 +34,7 @@ pub fn checksum_file<P: IsA<gio::File>, Q: IsA<gio::Cancellable>>(
 pub fn checksum_file_async<
     P: IsA<gio::File>,
     Q: IsA<gio::Cancellable>,
-    R: FnOnce(Result<Checksum, Box<dyn Error>>) + Send + 'static,
+    R: FnOnce(Result<Checksum, Box<dyn error::Error>>) + Send + 'static,
 >(
     f: &P,
     objtype: ObjectType,
@@ -42,7 +44,7 @@ pub fn checksum_file_async<
 ) {
     let user_data: Box<R> = Box::new(callback);
     unsafe extern "C" fn checksum_file_async_trampoline<
-        R: FnOnce(Result<Checksum, Box<dyn Error>>) + Send + 'static,
+        R: FnOnce(Result<Checksum, Box<dyn error::Error>>) + Send + 'static,
     >(
         _source_object: *mut gobject_sys::GObject,
         res: *mut gio_sys::GAsyncResult,
@@ -79,7 +81,8 @@ pub fn checksum_file_async_future<P: IsA<gio::File> + Clone + 'static>(
     f: &P,
     objtype: ObjectType,
     io_priority: i32,
-) -> Box_<dyn future::Future<Output = Result<Checksum, Box<dyn Error>>> + std::marker::Unpin> {
+) -> Box_<dyn future::Future<Output = Result<Checksum, Box<dyn error::Error>>> + std::marker::Unpin>
+{
     use fragile::Fragile;
     use gio::GioFuture;
 
@@ -100,7 +103,7 @@ pub fn checksum_file_from_input<P: IsA<gio::InputStream>, Q: IsA<gio::Cancellabl
     in_: Option<&P>,
     objtype: ObjectType,
     cancellable: Option<&Q>,
-) -> Result<Checksum, Box<dyn Error>> {
+) -> Result<Checksum, Box<dyn error::Error>> {
     unsafe {
         let mut out_csum = ptr::null_mut();
         let mut error = ptr::null_mut();
@@ -117,11 +120,43 @@ pub fn checksum_file_from_input<P: IsA<gio::InputStream>, Q: IsA<gio::Cancellabl
     }
 }
 
+#[cfg(any(feature = "v2017_13", feature = "dox"))]
+pub fn checksum_file_at<P: IsA<gio::Cancellable>>(
+    dfd: i32,
+    path: &std::path::Path,
+    stbuf: Option<&libc::stat>,
+    objtype: ObjectType,
+    flags: ChecksumFlags,
+    cancellable: Option<&P>,
+) -> Result<glib::GString, glib::Error> {
+    unsafe {
+        let mut out_checksum = ptr::null_mut();
+        let mut error = ptr::null_mut();
+        ostree_sys::ostree_checksum_file_at(
+            dfd,
+            path.to_glib_none().0,
+            stbuf
+                .map(|p| p as *const libc::stat as *mut libc::stat)
+                .unwrap_or(ptr::null_mut()),
+            objtype.to_glib(),
+            flags.to_glib(),
+            &mut out_checksum,
+            cancellable.map(|p| p.as_ref()).to_glib_none().0,
+            &mut error,
+        );
+        if error.is_null() {
+            Ok(from_glib_full(out_checksum))
+        } else {
+            Err(from_glib_full(error))
+        }
+    }
+}
+
 unsafe fn checksum_file_error(
     out_csum: *mut [*mut u8; 32],
     error: *mut glib_sys::GError,
     ret: i32,
-) -> Result<Checksum, Box<dyn Error>> {
+) -> Result<Checksum, Box<dyn error::Error>> {
     if !error.is_null() {
         Err(Box::<glib::Error>::new(from_glib_full(error)))
     } else if ret == GFALSE {
diff --git a/rust-bindings/rust/tests/functions/checksum_file_at.rs b/rust-bindings/rust/tests/functions/checksum_file_at.rs
new file mode 100644 (file)
index 0000000..7db7592
--- /dev/null
@@ -0,0 +1,27 @@
+use gio::NONE_CANCELLABLE;
+use ostree::{checksum_file_at, ChecksumFlags, ObjectType, RepoMode};
+use std::path::PathBuf;
+use util::TestRepo;
+
+#[test]
+fn should_checksum_file_at() {
+    let repo = TestRepo::new_with_mode(RepoMode::BareUser);
+    repo.test_commit("test");
+
+    let result = checksum_file_at(
+        repo.repo.get_dfd(),
+        &PathBuf::from(
+            "objects/89/f84ca9854a80e85b583e46a115ba4985254437027bad34f0b113219323d3f8.file",
+        ),
+        None,
+        ObjectType::File,
+        ChecksumFlags::IGNORE_XATTRS,
+        NONE_CANCELLABLE,
+    )
+    .expect("checksum file at");
+
+    assert_eq!(
+        result.as_str(),
+        "89f84ca9854a80e85b583e46a115ba4985254437027bad34f0b113219323d3f8",
+    );
+}
index 71b557cb4b9823db396126a722decf8bede9378b..3656d95d115b52908b80da4997582243781699e4 100644 (file)
@@ -1,8 +1,12 @@
 use gio::NONE_CANCELLABLE;
-use glib::Cast;
-use ostree::{checksum_file, checksum_file_from_input, ObjectType, RepoFile, RepoFileExt};
+use glib::prelude::*;
+use ostree::prelude::*;
+use ostree::{checksum_file, checksum_file_from_input, ObjectType, RepoFile};
 use util::TestRepo;
 
+#[cfg(feature = "v2017_13")]
+mod checksum_file_at;
+
 #[test]
 fn should_checksum_file() {
     let repo = TestRepo::new();